/**
 * libnlog -- User friendly logging system with network support.
 *
 * Copyright (C) 2011
 * 	Samuel Nussbaum <samuel (dot) nussbaum (at) e-robot.ch>
 *
 * This library is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 */
#include "Message.hpp"
#include "Logger.hpp"

#include <boost/thread/thread_time.hpp>
#include <boost/date_time/posix_time/ptime.hpp>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

#include <boost/date_time.hpp>

namespace nlog {

Message::Message(LevelEnum _level, const String &_name) :
	m_name(_name), m_timestamp(new Time(::boost::get_system_time())), m_level(_level) {
	/* Nothing to do... */
}

Message::Message(const Message &_other) :
	m_content(_other.m_content), m_name(_other.m_name), m_timestamp(new Time(*_other.m_timestamp)),
	m_level(_other.m_level) {
	/* Nothing to do... */
}

Message::~Message() {
	delete m_timestamp;
}

Message &Message::operator=(const Message &_other) {
	m_content = _other.m_content;
	m_name = _other.m_name;
	*m_timestamp = *_other.m_timestamp;
	m_level = _other.m_level;
	return *this;
}

bool Message::operator <(const Message &_other) const {
	return (*(m_timestamp) < *(_other.m_timestamp));
}

void Message::serialize(IArchive &_ar, const int) {
	String time;
	_ar & time;
	*m_timestamp = boost::posix_time::from_iso_string(time);

	_ar & m_level;
	_ar & m_content;
	_ar & m_name;
}

void Message::serialize(OArchive &_ar, const int) {
	String time(boost::posix_time::to_iso_string(*m_timestamp));
	_ar & time;
	_ar & m_level;
	_ar & m_content;
	_ar & m_name;
}

}
